import sqlite3, {Database} from 'sqlite3'
import fs from 'fs'

export enum ESqliteDBHandler {
  db="db", // 普通数据库
  tutorial="tutorial", // 教程
  game="game",
  user="user" // 用户数据
}
export default class Sqlite {
  // 不同的handler对应不同的数据库文件
  static dbHandler: Database
  static tutorialHandler: Database
  static gameHandler: Database
  static userHandler: Database
  static async connect (filePath?: string) {
    if (Sqlite.dbHandler) return
    if (filePath) {
      if (!fs.existsSync(filePath)) {
        Sqlite.dbHandler = null
        return
      }
      Sqlite.dbHandler = new sqlite3.Database(filePath)
    } else {
      Sqlite.dbHandler = null
    }
  }

  static async connectTutorial (filePath?: string) {
    if (Sqlite.tutorialHandler) return
    if (filePath) {
      if (!fs.existsSync(filePath)) {
        Sqlite.tutorialHandler = null
        return
      }
      Sqlite.tutorialHandler = new sqlite3.Database(filePath)
    } else {
      Sqlite.tutorialHandler = null
    }
  }

  static async connectGame (filePath?: string) {
    if (Sqlite.gameHandler) return
    if (filePath) {
      if (!fs.existsSync(filePath)) {
        Sqlite.gameHandler = null
        return
      }
      Sqlite.gameHandler = new sqlite3.Database(filePath)
    } else {
      Sqlite.gameHandler = null
    }
  }

  static async connectUser (filePath?: string) {
    if (Sqlite.userHandler) return
    if (filePath) {
      if (!fs.existsSync(filePath)) {
        Sqlite.userHandler = null
        return
      }
      Sqlite.userHandler = new sqlite3.Database(filePath)
    } else {
      Sqlite.userHandler = null
    }
  }

  static async select (sql: string, type: ESqliteDBHandler = ESqliteDBHandler.db): Promise<any> {
    return new Promise((resolve, reject) =>{
      if (type === ESqliteDBHandler.tutorial) {
        Sqlite.tutorialHandler.all(sql,function(err,row){
          if (err) {
            reject(err)
          }
          resolve(row)
        })
      } else if (type === ESqliteDBHandler.db)  {
        Sqlite.dbHandler.all(sql,function(err,row){
          if (err) {
            reject(err)
          }
          resolve(row)
        })
      } else if (type === ESqliteDBHandler.game) {
        Sqlite.gameHandler.all(sql,function(err,row){
          if (err) {
            reject(err)
          }
          resolve(row)
        })
      } else if (type === ESqliteDBHandler.user) {
        Sqlite.userHandler.all(sql,function(err,row){
          if (err) {
            reject(err)
          }
          resolve(row)
        })
      }
    })
  }

  static async dbOp (sql: string, type: ESqliteDBHandler = ESqliteDBHandler.db) {
    return new Promise((resolve, reject) =>{
      let handler
      if (type === ESqliteDBHandler.tutorial) handler = Sqlite.tutorialHandler
      else if (type === ESqliteDBHandler.db) handler = Sqlite.dbHandler
      else if (type === ESqliteDBHandler.game) handler = Sqlite.gameHandler
      else if (type === ESqliteDBHandler.user) handler = Sqlite.userHandler
      try {
        const prepare = handler.prepare(sql)
        prepare.run()
        // 用户表可能要导数据，不进行清空操作
        if (type !== ESqliteDBHandler.user) {
          const prepare2 = handler.prepare('vacuum')
          prepare2.run()
        }
        resolve(null)
      } catch (err) {
        reject(err)
      }
    })
  }

  static async  insert (sql: string) {
    try {
      return await Sqlite.dbOp(sql)
    } catch (err) {
      return null
    }
  }

  static async  delete (sql:string, type: ESqliteDBHandler) {
    return await Sqlite.dbOp(sql, type)
  }

  static async  update (sql:string) {
    return await Sqlite.dbOp(sql)
  }

  // 一般是建表才用这个方法
  static async  runSQL (sql:string) {
    return await Sqlite.dbOp(sql)
  }
  //TODO 没写完啊
  static async close(): Promise<any> {
    return this.dbHandler?.close()
  }

}
